<?php

class Application_Model_Settings extends Common_Model {

    public $tableName = 'Settings';

    const T_NUMBER = 0;
    const T_STRING = 1;
    const T_JSON = 2;

    public static $types = array(
        self::T_NUMBER => 'Numeric',
        self::T_STRING => 'String',
        self::T_JSON => 'JSON',
    );
    
    public function getByName($name){
        return $this->table->fetchRow('name=\''.$name.'\'');
    }

    public function getForm() {
        $form = parent::getForm();

        $name = new Zend_Form_Element_Text('name', array(
                    'label' => 'Nazwa',
                    'disabled' => 'disabled'
                ));
        $value = new Zend_Form_Element_Textarea('value', array(
                    'treść' => 'Wartość'
                ));

        $type = new Zend_Form_Element_Radio('type', array());
        $type->setLabel('Typ')
                ->addMultiOptions(self::$types)
                ->setSeparator('-');
        $type->setValue(self::T_STRING);

        $form->addDisplayGroup(array(
            $name, $type, $value
                ), 'gr-main');

        return $form;
    }
    
    public function define(){
        foreach($this->table->fetchAll() as $conf){
            if(!defined($conf->name)){
                define($conf->name, $conf->value);
            }
            
        }
    }

}

